package com.example.xyr.myapplication.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xyr.myapplication.Constant;
import com.example.xyr.myapplication.MyApp;
import com.example.xyr.myapplication.R;
import com.example.xyr.myapplication.base.RVBaseAdapter;
import com.example.xyr.myapplication.base.RVBaseHolder;
import com.example.xyr.myapplication.model.ChatBaseBean;
import com.example.xyr.myapplication.model.OpenRpBean;
import com.example.xyr.myapplication.model.RedPackageContent;
import com.example.xyr.myapplication.myinterface.MyUserAvatar;
import com.example.xyr.myapplication.ui.im.ChatActivity;
import com.example.xyr.myapplication.ui.im.ImageActivity;
import com.example.xyr.myapplication.ui.im.RpUserAvatar;
import com.example.xyr.myapplication.utils.DateUtils;
import com.example.xyr.myapplication.utils.EmotionUtils;
import com.example.xyr.myapplication.utils.LogUtil;
import com.example.xyr.myapplication.utils.MediaPlayerUtils;
import com.example.xyr.myapplication.utils.SpanStringUtils;
import com.example.xyr.myapplication.utils.StringUtil;
import com.example.xyr.myapplication.utils.TextUtil;
import com.example.xyr.myapplication.view.CustomImageView;
import com.google.gson.jpush.Gson;
import com.jrmf360.rplib.JrmfRpClient;
import com.jrmf360.rplib.bean.GrabRpBean;
import com.jrmf360.rplib.utils.callback.GrabRpCallBack;

import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.enums.MessageDirect;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * @创建人 honglin
 * @创建时间 2018/4/24 下午Constant.SeparatedTime:03
 * @类描述 聊天页面adapter
 */
public class ChatAdapter extends RVBaseAdapter<Message, RVBaseHolder> {


    private ChatActivity mAttachActivity;


    public ChatAdapter(ChatActivity attachActiviy) {
        this.mAttachActivity = attachActiviy;
    }

    @Override
    public RVBaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RVBaseHolder holder = null;
        switch (viewType) {
            case ChatType.TYPE_SEND_Text:
                //发送文本
                View textView = LayoutInflater.from(parent.getContext()).inflate(R.layout.jmui_chat_item_send_text, parent, false);
                holder = new SendTextHolder(textView);
                break;
            case ChatType.TYPE_RECEIVE_Text:
                //收文本信息
                View rpReceiveView = LayoutInflater.from(parent.getContext()).inflate(R.layout.jmui_chat_item_receive_text, parent, false);
                holder = new ReceiveTextHodler(rpReceiveView);
                break;
            case ChatType.TYPE_SEND_CUSTOM:
                //发语音
                View sendCustomView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item_send_custom, parent, false);
                holder = new SendVoice(sendCustomView);
                break;
            case ChatType.TYPE_RECEIVE_CUSTOM:
                //接收语音
                View receiveCustomView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item_receive_custom, parent, false);
                holder = new ReceiveVoice(receiveCustomView);
                break;
            case ChatType.TYPE_SEND_RP:
                //发红包
                View sendRPView = LayoutInflater.from(parent.getContext()).inflate(R.layout.jrmf_rp_bribery_item_send, parent, false);
                holder = new SendRedPackage(sendRPView);
                break;
            case ChatType.TYPE_RECEIVE_RP:
                //收红包
                View receiveRPView = LayoutInflater.from(parent.getContext()).inflate(R.layout.jrmf_rp_bribery_item_receive, parent, false);
                holder = new ReceiveRedPackage(receiveRPView);
                break;
            case ChatType.TYPE_OPEN_RP:
                View openRPView = LayoutInflater.from(parent.getContext()).inflate(R.layout.jrmf_rp_open_packet, parent, false);
                holder = new OpRedPackage(openRPView);
                break;
            case ChatType.TYPE_SEND_IMAGE:
                //发送图片
                View sendImageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item_send_image, parent, false);
                holder = new SendImage(sendImageView);
                break;
            case ChatType.TYPE_RECEIVE_IMAGE:
                //接收图片
                View receiveImageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item_receive_image, parent, false);
                holder = new ReceiveImage(receiveImageView);
                break;
            case ChatType.TYPE_NULL:
                LogUtil.e("tag", "空消息  ");
                break;
        }
        return holder;
    }


    @Override
    public int getItemViewType(int position) {
        Message message = mDatas.get(position);
        if (message.getContentType() == ContentType.text) { //文本
            if (message.getDirect() == MessageDirect.send) {
                return ChatType.TYPE_SEND_Text;
            } else {
                return ChatType.TYPE_RECEIVE_Text;
            }
        } else if (message.getContentType() == ContentType.voice) { //语音
            if (message.getDirect() == MessageDirect.send) {
                return ChatType.TYPE_SEND_CUSTOM;
            } else {
                return ChatType.TYPE_RECEIVE_CUSTOM;
            }
        } else if (message.getContentType() == ContentType.custom) {  //红包
            ChatBaseBean chatBaseBean = new Gson().fromJson(message.getContent().toJson(), ChatBaseBean.class);
            if (chatBaseBean.customType.equals(Constant.CUSTOM_REDPACKAGE)) {
                if (message.getDirect() == MessageDirect.send) {
                    return ChatType.TYPE_SEND_RP;  //发红包
                } else {
                    return ChatType.TYPE_RECEIVE_RP;  //收红包
                }
            } else if (chatBaseBean.customType.equals(Constant.CUSTOM_OPEN_REDPACKAGE)) {
                return ChatType.TYPE_OPEN_RP;   //拆红包
            }
        } else if (message.getContentType() == ContentType.image) {  //图片
            if (message.getDirect() == MessageDirect.send) {
                return ChatType.TYPE_SEND_IMAGE;  //发图片
            } else {
                return ChatType.TYPE_RECEIVE_IMAGE;  //收图片
            }
        }
        return ChatType.TYPE_NULL;
    }

    //发送文本
    class SendTextHolder extends RVBaseHolder {
        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_msg_content)
        TextView tvMsg;
        @BindView(R.id.jmui_sending_iv)
        ImageView jmuiSendingIv;
        @BindView(R.id.jmui_fail_resend_ib)
        ImageButton jmuiFailResendIb;

        public SendTextHolder(View itemView) {
            super(itemView);

        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            Message message = mDatas.get(adapterPosition);
            TextContent content = (TextContent) message.getContent();
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            if (StringUtil.isNotEmpty(content.getText())) {
                tvMsg.setText(SpanStringUtils.getEmotionContent(EmotionUtils.EMOTION_CLASSIC_TYPE,
                        mAttachActivity, tvMsg, content.getText()));
                if (message.getFromUser() != null) {
                    message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                        @Override
                        public void gotResult(int i, String s, Bitmap bitmap) {
                            if (i == 0) {
                                avatarIv.setImageBitmap(bitmap);
                            }
                        }
                    });
                }
            }
        }
    }

    //接收文本
    class ReceiveTextHodler extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_display_name_tv)
        TextView jmuiDisplayNameTv;
        @BindView(R.id.jmui_msg_content)
        TextView tvMsg;

        public ReceiveTextHodler(View itemView) {
            super(itemView);
        }


        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            Message message = mDatas.get(adapterPosition);
            TextContent content = (TextContent) message.getContent();
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            if (StringUtil.isNotEmpty(content.getText())) {
                tvMsg.setText(SpanStringUtils.getEmotionContent(EmotionUtils.EMOTION_CLASSIC_TYPE,
                        mAttachActivity, tvMsg, content.getText()));
                if (message.getFromUser() != null) {
                    message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                        @Override
                        public void gotResult(int i, String s, Bitmap bitmap) {
                            if (i == 0) {
                                avatarIv.setImageBitmap(bitmap);
                            }
                        }
                    });
                }
            }
        }
    }

    //发送语音
    class SendVoice extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_msg_content)
        TextView tvCustom;
        @BindView(R.id.iv_custom)
        ImageView iv_custom;
        @BindView(R.id.tv_duration)
        TextView tvDuration;

        MediaPlayerUtils mediaPlayerUtils = MediaPlayerUtils.getInstance();

        public SendVoice(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            Message message = mDatas.get(adapterPosition);
            final VoiceContent content = new Gson().fromJson(message.getContent().toJson(), VoiceContent.class);
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            if (message.getFromUser() != null) {
                message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            avatarIv.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            DecimalFormat format = new DecimalFormat("0.#");
            String duration = format.format(Double.valueOf(content.getDuration()) / 1000);
            tvDuration.setText(duration);
            //点击播放语音
            tvCustom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StringUtil.isNotEmpty(content.getLocalPath())) {
                        if (mediaPlayerUtils.isPlay()) {
                            mediaPlayerUtils.stop();
                        } else {
                            mediaPlayerUtils.setImageAnim(iv_custom);
                            mediaPlayerUtils.playUrl(content.getLocalPath());
                        }
                    }
                }
            });
        }
    }

    //接收语音
    class ReceiveVoice extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_msg_content)
        TextView tvCustom;
        @BindView(R.id.iv_custom)
        ImageView iv_custom;
        @BindView(R.id.tv_duration)
        TextView tvDuration;
        MediaPlayerUtils mediaPlayerUtils = MediaPlayerUtils.getInstance();

        public ReceiveVoice(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            Message message = mDatas.get(adapterPosition);
            final VoiceContent content = new Gson().fromJson(message.getContent().toJson(), VoiceContent.class);
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            if (message.getFromUser() != null) {
                message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            avatarIv.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            DecimalFormat format = new DecimalFormat("0.#");
            String duration = format.format(Double.valueOf(content.getDuration()) / 1000);
            tvDuration.setText(duration);
            //点击播放语音
            tvCustom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mediaPlayerUtils.isPlay()) {
                        mediaPlayerUtils.stop();
                    } else {
                        mediaPlayerUtils.setImageAnim(iv_custom);
                        mediaPlayerUtils.playUrl(content.getLocalPath());
                    }

                }
            });

        }

    }

    //发送图片
    class SendImage extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_msg_image)
        ImageView ivImage;

        public SendImage(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            final Message message = mDatas.get(adapterPosition);
            final ImageContent content = new Gson().fromJson(message.getContent().toJson(), ImageContent.class);
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            if (message.getFromUser() != null) {
                message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            avatarIv.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            String thumbLocalPath = content.getLocalThumbnailPath();
            if (StringUtil.isNotEmpty(thumbLocalPath)) {
                Bitmap bitmap = BitmapFactory.decodeFile(thumbLocalPath);
                ivImage.setImageBitmap(bitmap);
                ivImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageActivity.Companion.initActivity(mAttachActivity, message);
                    }
                });
            }
        }
    }

    //收图片
    class ReceiveImage extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_msg_image)
        ImageView ivImage;

        public ReceiveImage(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            final Message message = mDatas.get(adapterPosition);
            final ImageContent content = new Gson().fromJson(message.getContent().toJson(), ImageContent.class);
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            if (message.getFromUser() != null) {
                message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            avatarIv.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            String thumbLocalPath = content.getLocalThumbnailPath();
            LogUtil.e("tag", "图片》》》》》" + thumbLocalPath);
            if (StringUtil.isNotEmpty(thumbLocalPath)) {
                Bitmap bitmap = BitmapFactory.decodeFile(thumbLocalPath);
                ivImage.setImageBitmap(bitmap);
                ivImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageActivity.Companion.initActivity(mAttachActivity, message);
                    }
                });
            }
        }
    }


    //发送红包
    class SendRedPackage extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.tv_bri_mess)
        TextView tvBriMess;
        @BindView(R.id.tv_bri_target)
        TextView tvBriTarget;
        @BindView(R.id.tv_bri_name)
        TextView tvBriName;
        @BindView(R.id.bri_bg)
        RelativeLayout briBg;

        public SendRedPackage(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            final Message message = mDatas.get(adapterPosition);
            CustomContent content = (CustomContent) message.getContent();
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            final RedPackageContent redPackageContent = new Gson().fromJson(content.toJson(), RedPackageContent.class);
            if (StringUtil.isNotEmpty(redPackageContent.getEnvelopeMessage())) {
                tvBriMess.setText(redPackageContent.getEnvelopeMessage());
            }
            if (StringUtil.isNotEmpty(redPackageContent.getEnvelopeName())) {
                tvBriName.setText(redPackageContent.getEnvelopeName());
            }
            if (message.getFromUser() != null) {
                message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            avatarIv.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            //查看红包
            briBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StringUtil.isNotEmpty(redPackageContent.getEnvelopesID())) {
                        JrmfRpClient.openSingleRp(mAttachActivity, MyApp.userId, MyApp.token, message.getFromUser().getNickname(), message.getFromUser().getAvatar(), redPackageContent.getEnvelopesID(), new GrabRpCallBack() {
                            @Override
                            public void grabRpResult(GrabRpBean grabRpBean) {

                            }
                        });
                    }
                }
            });
        }

    }

    //收红包
    class ReceiveRedPackage extends RVBaseHolder implements MyUserAvatar {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.jmui_avatar_iv)
        CustomImageView avatarIv;
        @BindView(R.id.jmui_display_name_tv)
        TextView jmuiDisplayNameTv;
        @BindView(R.id.tv_bri_mess)
        TextView tvBriMess;
        @BindView(R.id.tv_bri_target)
        TextView tvBriTarget;
        @BindView(R.id.tv_bri_name)
        TextView tvBriName;
        @BindView(R.id.bri_bg)
        RelativeLayout briBg;
        @BindView(R.id.layout)
        RelativeLayout layout;

        private UserInfo userInfo;

        public ReceiveRedPackage(View itemView) {
            super(itemView);
            RpUserAvatar.getInstent().setMyUserAvatar(this);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            final Message message = mDatas.get(adapterPosition);
            CustomContent content = (CustomContent) message.getContent();
            userInfo = message.getFromUser();
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            final RedPackageContent redPackageContent = new Gson().fromJson(content.toJson(), RedPackageContent.class);
            if (StringUtil.isNotEmpty(redPackageContent.getEnvelopeMessage())) {
                tvBriMess.setText(redPackageContent.getEnvelopeMessage());
            }
            if (StringUtil.isNotEmpty(redPackageContent.getEnvelopeName())) {
                tvBriName.setText(redPackageContent.getEnvelopeName());
            }
            if (message.getFromUser() != null) {
                message.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            avatarIv.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            //查看红包
            briBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StringUtil.isNotEmpty(redPackageContent.getEnvelopesID())) {
                        JrmfRpClient.openSingleRp(mAttachActivity, MyApp.userId, MyApp.token, message.getFromUser().getNickname(), message.getFromUser().getAvatar(), redPackageContent.getEnvelopesID(), new GrabRpCallBack() {
                            @Override
                            public void grabRpResult(GrabRpBean grabRpBean) {
                                if (grabRpBean.isHadGrabRp()) {
                                    //发送抢红包的消息
                                    Map<String, String> map = new HashMap();
                                    map.put("hasLeft", grabRpBean.getHasLeft() + "");
                                    map.put("total", grabRpBean.getTotal() + "");
                                    map.put("totalMoney", grabRpBean.getTotalMoney());
                                    map.put("grabMoney", grabRpBean.getGrabMoney());
                                    map.put("openUserId", MyApp.userId);
                                    map.put("envelopesID", redPackageContent.getEnvelopesID());
                                    map.put("fromeRpName", message.getFromUser().getNickname());
                                    map.put("customType", Constant.CUSTOM_OPEN_REDPACKAGE);
                                    Message singleCustomMessage = JMessageClient.createSingleCustomMessage(mAttachActivity.getOtherId(), Constant.APPKEY, map);
                                    if (singleCustomMessage != null) {
                                        JMessageClient.sendMessage(singleCustomMessage);
                                        addItem(singleCustomMessage);
                                    }
                                }
                            }
                        });
                    }
                }
            });
        }

        @Override
        public void setAvatar(final ImageView view) {
            LogUtil.e("tag", "" + view);
            if (userInfo != null) {
                userInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (i == 0) {
                            view.setImageBitmap(bitmap);
                        }
                    }
                });
            }
        }
    }

    //拆红包
    class OpRedPackage extends RVBaseHolder {

        @BindView(R.id.jmui_send_time_txt)
        TextView timeTxt;
        @BindView(R.id.packet_message)
        TextView packetMessage;


        public OpRedPackage(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(RVBaseHolder holder, int adapterPosition) {
            final Message message = mDatas.get(adapterPosition);
            String nickname = message.getFromUser().getNickname();
            final OpenRpBean openRpBean = new Gson().fromJson(message.getContent().toJson(), OpenRpBean.class);
            final UserInfo myInfo = JMessageClient.getMyInfo();
            String time = DateUtils.getTimeDisplay(message.getCreateTime());
            if (adapterPosition > 0) {
                long lastTime = mDatas.get(adapterPosition - 1).getCreateTime();
                long intervalMinutes = DateUtils.getIntervalMinutes(message.getCreateTime(), lastTime);
                if (intervalMinutes > Constant.SeparatedTime) {
                    timeTxt.setText(time);
                }
            } else {
                timeTxt.setText(time);
            }
            String msg;
            if (MyApp.userId.equals(String.valueOf(openRpBean.openUserId))) {
                msg = "你领取了" + openRpBean.getFromeRpName() + "的红包";
            } else {
                msg = nickname + "领取了你的红包";
            }
            int color = mAttachActivity.getResources().getColor(R.color.my_pink);
            SpannableStringBuilder builder = TextUtil.Companion.getInstance()
                    .getColorText(msg, color, msg.length() - 2, msg.length(),
                            new TextUtil.ClickableSpanLis() {
                @Override
                public void onClick(@NotNull View widget) {
                    JrmfRpClient.openRpDetail(mAttachActivity, MyApp.userId,
                            MyApp.token, openRpBean.getEnvelopesID(), myInfo.getNickname(),
                            myInfo.getAvatar());
                }
            });
            packetMessage.setText(builder);
            packetMessage.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }

}








